#!/bin/sh
# Bug reports and comments to Igor.Ermolaev@intel.com
base=$(dirname $(readlink -f ${BASH_SOURCE[0]:-$0}))
atool=$base/sde/sde
if [ `uname -m` == x86_64 ] && [ -x ${atool}64 ];then atool=${atool}64; fi
etool=$atool
cur_rank=${PMI_RANK:-$MPD_JRANK};cur_rank=${cur_rank:-$MPIRUN_RANK};cur_rank=${cur_rank:-$PMI_ID}
rank=0
native=false
fixed=false
action=run
mp_mode=false
mt_mode=false
delay=10
dir_reqd=false
repeat=false
local_sptd=false
unset comm args largs tgt mdl_name mdl_addr env tid logs replay stdin pause strt_rmrk strt_cond strt_cntl cur_cond stop_rmrk stop_cond stop_cntl log_shm rt_cntl
while [ $# -ne 0 ] && [ "$1" != '--' ];do
   case "$1" in
   --local--)
      args="${args:+$args }""$1"
      local_sptd=true
      shift ;;
   -mpi[-_]rank)
      rank=$2
      [ -z "$cur_rank" ] || [ "$rank" == "$cur_rank" ] || set +x
      if [ $local_sptd == true ];then largs="${largs:+$largs }""$1 $2"; fi
      shift 2 ;;
   -delay)
      delay=$2
      if [ $local_sptd == true ];then largs="${largs:+$largs }""$1 $2"; fi
      shift 2 ;;
   -native[-_]run)
      native=true
      if [ $local_sptd == true ];then largs="${largs:+$largs }""$1"; fi
      shift ;;
   -comm[-_]channel)
      comm="$2"
      if [ $local_sptd == true ];then largs="${largs:+$largs }""$1 $2"; fi
      shift 2 ;;
   -fixed[-_]layout)
      fixed=true
      if [ $local_sptd == true ];then largs="${largs:+$largs }""$1"; fi
      shift ;;
   -moi)
      mdl_name=${2%%:[0-9]*};mdl_addr=${2##*:}
      if [ "$mdl_name" == "$mdl_addr" ];then
         mdl_name=${2%%+[xX0-9a-fA-F]*};mdl_addr=${2##*+}
         if [ "$mdl_name" == "$mdl_addr" ];then unset mdl_addr;else mdl_addr="-$mdl_addr"; fi
      fi
      if [ $local_sptd == true ];then largs="${largs:+$largs }""$1 $2"; fi
      shift 2 ;;
   -store[-_]env)
      env=environment.txt
      if [ $local_sptd == true ];then largs="${largs:+$largs }""$1"; fi
      shift ;;
   -tid)
      tid="$2"
      if [ $local_sptd == true ];then largs="${largs:+$largs }""$1 $2"; fi
      shift 2 ;;
   -mt)
      mt_mode=true
      if [ $local_sptd == true ];then largs="${largs:+$largs }""$1"; fi
      shift ;;
   -mp)
      mp_mode=true
      if [ $local_sptd == true ];then largs="${largs:+$largs }""$1"; fi
      shift ;;
   -shm)
      if [ "$2" == 0 ];then 
         log_shm=false
         if [ $local_sptd == true ];then largs="${largs:+$largs }""$1 $2"; fi
         shift 2
      else
         log_shm=true
         if [ $local_sptd == true ];then largs="${largs:+$largs }""$1"; fi
         shift
      fi
      ;;
   -pinlit2)
      action=pinlit2
      args="${args:+$args }""$1 -log:compressed gzip"
      shift ;;
   -pinlit2[-_]replay64)
      args="${args:+$args }""-pinlit2-replay64 $2"
      replay="$2"
      shift 2 ;;
   -log:LIT[-_]warmup)
      action=warmup
      args="${args:+$args }"'-pinlit2 -log:LIT-warmup -log:LIT-warmup-filter-size 16 -log:compressed gzip'
      shift ;;
   -record)
      action=record
      args="${args:+$args }"'-log -log:pages-early 1 -log:basename rgn.pinball/record -log:compressed bzip2'
      shift ;;
   -mix)
      action=mix
      logs="${logs:+$logs }"'-omix:mix-out.txt'
      args="${args:+$args }""$1 -iform -demangle 0 -mapaddr-top-blocks -global-hot-blocks 0 -global-functions 0"
      shift ;;
   -icount)
      action=stats
      logs="${logs:+$logs }"'-icount-file:icount.out'
      args="${args:+$args }""$1"
      shift ;;
   -align[-_]checker)
      action=stats
      logs="${logs:+$logs }"'-align-checker-file:align-checker.out'
      args="${args:+$args }""$1 count-all -align-checker-prefetch 0"
      shift ;;
   -clc)
      action=stats
      logs="${logs:+$logs }"'-clc-out:sde-memory-area-crossing.out.txt'
      args="${args:+$args }""$1 -clc-avx -pgc"
      shift ;;
   -pause[-_]delay)
      pause="-pause-delay $2"
      shift 2 ;;
   -t)
      if [ "$2" == supermadd.so ];then
         action=stats
         logs="${logs:+$logs }"'-o:supermadd.out'
      fi
      args="${args:+$args }""$1 $2"
      shift 2 ;;
   -stdin)
      stdin="$2"
      shift 2 ;;
   -waitfor[-_]address)
      cur_cond=${cur_cond:+$cur_cond,}'precond:address:'`echo $2|sed 's/:\([0-9]\)/:count\1/'`
      largs="${largs:+$largs }""$1 $2"
      shift 2 ;;
   -waitfor[-_]icount)
      cur_cond=${cur_cond:+$cur_cond,}"precond:icount:$2"
      largs="${largs:+$largs }""$1 $2"
      shift 2 ;;
   -start[-_]address)
      strt_cond="$cur_cond";unset cur_cond
      strt_cntl=${strt_cntl:+$strt_cntl }'-control start:address:'`echo $2|sed 's/:\([0-9]\)/:count\1/'`',name:c1,waitfor:c0'
      largs="${largs:+$largs }""$1 $2"
      shift 2 ;;
   -stop[-_]address)
      stop_cond="$cur_cond";unset cur_cond
      stop_cntl=${stop_cntl:+$stop_cntl }'-control stop:address:'`echo $2|sed 's/:\([0-9]\)/:count\1/'`',waitfor:c2'
      largs="${largs:+$largs }""$1 $2"
      shift 2 ;;
   -skip)
      strt_cond="$cur_cond";unset cur_cond
      strt_cntl=${strt_cntl:+$strt_cntl }"-control start:icount:$(($2+1)),name:c1,waitfor:c0"
      largs="${largs:+$largs }""$1 $2"
      shift 2 ;;
   -length)
      stop_cond="$cur_cond";unset cur_cond
      stop_cntl=${stop_cntl:+$stop_cntl }"-control stop:icount:$2,waitfor:c2"
      largs="${largs:+$largs }""$1 $2"
      shift 2 ;;
   -repeat)
      repeat=true
      largs="${largs:+$largs }""$1"
      shift ;;
   -lrb2|-knc)
      tgt="${tgt:+$tgt }"'-knc'
      atool=$base/sde.lrb2/mic-sde
      shift ;;
   -icl|-skl|-skx|-knl|-knh|-knh-sc|-knh+|-snb|-ivb|-hsw|-bdw|-nhm|-wsm)
      tgt="${tgt:+$tgt }""$1"
      shift ;;
   -profile)
      action=profile
      atool="$base/get_prf $2 $3"
      unset etool
      shift 3 ;;
   -profile[-_]mix)
      action=profile-mix
      atool="$base/get_prf $2 $3 -mix $base/sde_mpi"
      shift 3 ;;
   -runtime[-_]control)
      re='^[Rr][0-9]+(:[0-9]+)?$';if [ -n "$2" ] && [[ $2 =~ $re ]];then
         rt_cntl="$2";flds=(${rt_cntl//:/ });flds[0]=${flds[0]:1}
         if [ ${flds[0]} == 0 ];then __SDE_TBD_NAME=any;flds[0]=2abb; else __SDE_TBD_NAME=${flds[0]};flds[0]=$(printf '%x' ${flds[0]}); fi
         strt_rmrk=${flds[0]};stop_rmrk=$((0x$strt_rmrk+0x8000));stop_rmrk=$(printf '%x' $stop_rmrk)
         if [ -n "${flds[1]}" ] && [ "${flds[1]}" != 1 ];then strt_rmrk="$strt_rmrk:count${flds[1]}"; fi
         unset flds
         largs="${largs:+$largs }""$1 $2"
         shift 2
      else
         rt_cntl='enable'
         __SDE_TBD_NAME=any
         strt_rmrk=2abb;stop_rmrk=aabb
         largs="${largs:+$largs }""$1"
         shift
      fi
      ;;
   -no[-_]resolve)
      largs="${largs:+$largs }""$1"
      shift ;;
   -events[-_]set)
      largs="${largs:+$largs }""$1 $2"
      shift 2 ;;
   *)
      args="${args:+$args }""$1"
      shift ;;
   esac
done
largs="${largs:+$largs }"`echo $args|sed -n 's/.*--local--\s*//p'`
args=`echo $args|sed -e 's/\(^\|\s\)--local--\($\|\s\)/\2/' -e 's/^\s\+//'`
if [ -n "$replay" ];then unset cur_rank; fi
if [ $action == profile ] || [ $action == profile-mix ];then log_shm=false; fi
if [ -z "$log_shm" ];then
   if [ $action == pinlit2 ] || [ $action == record ];then
      if [ $mp_mode == true ];then
         log_shm=true
      elif [ -n "$cur_rank" ];then
         #if [ "$I_MPI_FABRICS" != tcp ];then log_shm=true; fi #slow
         log_shm=false
      fi
   fi
   if [ -z "$log_shm" ];then log_shm=false; fi
fi
if [ -n "$mdl_addr" ] && [ ${mdl_addr:0:1} != - ];then
   args=`echo $args|awk -F '-address ' '{ORS="";print $1;for(i=2;i<=NF;i++){print"-address ";split($i,A,":");split(A[1],B," ");printf("0x%x",strtonum(B[1])+'$mdl_addr');for(k in B){if(k!=1){print" "B[k]}}for(j in A){if(j!=1){print":"A[j]}}}ORS="\n";print""}'`
fi
if ( [ $action == pinlit2 ] || [ $action == record ] ) && [[ ! $args =~ -pinplay:max[-_]threads ]];then args="${args:+$args }"'-pinplay:max-threads 65'; fi
if ( [ $action == pinlit2 ] || [ $action == warmup ] ) && [[ ! $args =~ -log:LIT[-_]memory[-_]base ]];then args="${args:+$args }"'-log:LIT_memory_base 0x400000000 -log:LIT_memory_ceil 0x2000000000 -log:LIT_paging_map random'; fi
if [ $action == pinlit2 ] || [ $action == warmup ];then
   if [[ ! $args =~ -log:basename ]];then args="${args:+$args }""-log:basename ${replay:-rgn.pinball/pinlit64}"; fi
   if [ -n "$replay" ];then
      args=`echo $args|sed -e 's/-pinlit2\s*\|-pinlit2-replay64\s\+\S\+\s*//g' -e 's|-log:basename|-pinlit2-relog64 '"$replay"'|'`
   fi
fi
if [ -n "$replay" ] && [ -s "$replay.ptov" ] && [[ ! $args =~ -log:LIT[-_]use[-_]ptov ]];then
   args="${args:+$args }""-log:LIT-use-ptov $replay.ptov"
fi
if [ $log_shm == true ];then
   args="${args:+$args }"'-log:mp-mode -log:mp-attach'
fi
if [ -n "$strt_rmrk" ];then strt_cond="precond:ssc:$strt_rmrk:runtime"${strt_cond:+,$strt_cond};unset strt_rmrk; fi
if [ -z "$strt_cntl" ] && [ -n "$strt_cond" ];then
   strt_cntl='-control start:'"${strt_cond##*precond:}"',name:c1,waitfor:c0'
   strt_cond=${strt_cond%precond:*};strt_cond=${strt_cond%,}
fi
if [ -n "$stop_rmrk" ] && [ -z "$stop_cond" ] && [ -z "$stop_cntl" ];then stop_cntl="-control stop:ssc:$stop_rmrk:runtime,waitfor:c2";unset stop_rmrk; fi
if [ -z "$replay" ] && ( [ $mt_mode == true ] || [ -z "$tid" ] );then
   cntl_desc=( `echo $strt_cntl|sed -n 's/^.*start:\(address:[^:,]\+:count\)\([0-9]\+\).*$/\1 \2/p'` )
   if [ -n "${cntl_desc[0]}" ] && [ "${cntl_desc[1]}" != 1 ];then
      strt_cond=${strt_cond:+$strt_cond,}"precond:${cntl_desc[0]}$((cntl_desc[1]-1)):bcast"
      strt_cntl=`echo $strt_cntl|sed 's/\(start:address:[^:,]\+\)\(:count[0-9]\+\)/\1/'`
   fi
   if [ -z "$strt_cntl" ];then
      cntl_desc=( `echo $stop_cntl|sed -n 's/^.*stop:\(address:[^:,]\+:count\)\([0-9]\+\).*$/\1 \2/p'` )
      if [ -n "${cntl_desc[0]}" ] && [ "${cntl_desc[1]}" != 1 ];then
         stop_cond=${stop_cond:+$stop_cond,}"precond:${cntl_desc[0]}$((cntl_desc[1]-1)):bcast"
         stop_cntl=`echo $stop_cntl|sed 's/\(stop:address:[^:,]\+\)\(:count[0-9]\+\)/\1/'`
      fi
   fi
   unset cnd_desc
fi
if [ -n "$replay" ];then
   if [ -f "$replay.global.log" ];then
      global="$replay.global.log"
   elif [ -n "$tid" ] && [ -f "$replay.$tid.global.log" ];then
      global="$replay.$tid.global.log"
   else
      global="$replay.0.global.log"
   fi
   if [ -f "$replay.result" ];then
      result="$replay.result"
   elif [ -n "$tid" ] && [ -f "$replay.$tid.result" ];then
      result="$replay.$tid.result"
   else
      result="$replay.0.result"
   fi
fi
if [ -n "$replay" ] && ( [ $action == pinlit2 ] || [ $action == record ] );then
   if [[ ! $args =~ -log:whole[-_]image ]];then
      src_cmd=`grep 'pin-command-line:' "$result"`
      if [ -z "$src_cmd" ];then
         src_cmd=`grep 'pin-command-line:' "$global"`
      fi
      if [ -n "$src_cmd" ];then
         args="${args:+$args }"`echo "$src_cmd"|sed -n 's/^.*\s\(-log:whole[-_]image\(\s\+[0-9]\+\)\?\).*$/\1/p'`
      fi
      unset src_cmd
   fi
fi
if [ -z "$strt_cntl" ];then
   strt_cntl='-control start:icount:1,name:c1,waitfor:c0'
fi
if [ -z "$strt_cond" ];then 
   strt_cntl=${strt_cntl//,waitfor:c0/}
else
   strt_cond="-control $strt_cond,name:c0"
fi
if [ -z "$stop_cond" ];then 
   stop_cntl=${stop_cntl//,waitfor:c2/,waitfor:c1}
else
   stop_cond="-control $stop_cond,name:c2,waitfor:c1"
fi
if [ $mt_mode == true ];then
   if [ -n "$tid" ];then
      strt_cntl=${strt_cntl//,name:c/:tid$tid:bcast,name:c}
      stop_cntl=${stop_cntl//,waitfor:c/:tid$tid:bcast,waitfor:c}

      strt_cond=`echo $strt_cond|sed 's/\(precond:icount:[0-9]\+\)/\1:tid'$tid'/g'`
      stop_cond=`echo $stop_cond|sed 's/\(precond:icount:[0-9]\+\)/\1:tid'$tid'/g'`
   else
      strt_cntl=${strt_cntl//,name:c/:bcast,name:c}
      stop_cntl=${stop_cntl//,waitfor:c/:bcast,waitfor:c}
   fi
elif [ -n "$tid" ];then
   strt_cntl=${strt_cntl//,name:c/:tid$tid,name:c}
   stop_cntl=${stop_cntl//,waitfor:c/:tid$tid,waitfor:c}

   strt_cond=${strt_cond//,precond:/:tid$tid,precond:}
   strt_cond=${strt_cond//,name:c/:tid$tid,name:c}
   stop_cond=${stop_cond//,precond:/:tid$tid,precond:}
   stop_cond=${stop_cond//,name:c/:tid$tid,name:c}

   if [ $action == pinlit2 ] || [ $action == record ] || [ -n "$replay" ];then
      args="${args:+$args }""-log:focus-thread $tid"
   fi
fi
strt_cntl=`echo $strt_cntl|sed 's/\(ssc\:[0-9a-fA-F]\+\(:count[0-9]\+\)\?\)\:runtime\(\:tid[0-9]\+\)\?\(\:bcast\)\?/\1\4/'`
stop_cntl=`echo $stop_cntl|sed 's/\(ssc\:[0-9a-fA-F]\+\(:count[0-9]\+\)\?\)\:runtime\(\:tid[0-9]\+\)\?\(\:bcast\)\?/\1\4/'`
strt_cond=`echo $strt_cond|sed 's/\(ssc\:[0-9a-fA-F]\+\(:count[0-9]\+\)\?\)\:runtime\(\:tid[0-9]\+\)\?\(\:bcast\)\?/\1:bcast/'`
stop_cond=`echo $stop_cond|sed 's/\(ssc\:[0-9a-fA-F]\+\(:count[0-9]\+\)\?\)\:runtime\(\:tid[0-9]\+\)\?\(\:bcast\)\?/\1:bcast/'`
if [ $repeat != true ] && [ -n "$stop_cntl" ];then
   re='\s-(log:|\w+-)early-out';if [[ ! $args =~ $re ]];then args="${args:+$args }"'-early-out'; fi
fi
# Squeeze
if [ -n "$strt_cond" ] && [ `echo $strt_cntl|grep -o '\-control'|wc -l` -eq 1 ];then
   strt_cntl=`echo $strt_cond $strt_cntl|sed -e 's/,waitfor:c[0-9]\+//g' -e 's/,name:c[0-9]\+\s/ /g' -e 's/\s\+-control\s\+/,/g'`
   unset strt_cond
fi
if [ -n "$stop_cond" ] && [ `echo $stop_cntl|grep -o '\,waitfor:c2'|wc -l` -eq 1 ];then
   stop_cntl=`echo $stop_cond $stop_cntl|sed -e 's/,name:c2,waitfor:c1/ /g' -e 's/\s\+-control\s\+precond:/,precond:/g' -e 's/\s\+-control\s\+\(\S\+\),waitfor:c2/,\1,waitfor:c1/g'`
   unset stop_cond
fi
if [ `echo $strt_cntl|grep -o '\-control'|wc -l` -le 1 ] && [ `echo $stop_cntl|grep -o '\-control'|wc -l` -le 1 ];then
   strt_cntl=`echo $strt_cond $strt_cntl $stop_cond $stop_cntl|sed -e 's/,\(name\|waitfor\):c[0-9]\+//g' -e 's/\s\+-control\s\+/,/g'`
else
   strt_cntl="${strt_cond:+$strt_cond }${strt_cntl:+$strt_cntl }${stop_cond:+$stop_cond }$stop_cntl"
fi
if [ `echo $strt_cntl|grep -o '\-control'|wc -l` -eq 1 ];then
   strt_cntl=`echo $strt_cntl|sed 's/^-control\s\+start:icount:\(0\|1\)\(:tid[0-9]\)\?\+\(,\(stop\)\|$\)/-control \4/'`
fi
if [ "$strt_cntl" == '-control ' ];then unset strt_cntl; fi
if [ $repeat == true ];then
   if [ -n "$stop_cntl" ];then
      stop_cntl=`echo $stop_cntl|sed 's/\(.\)\(\s\+-control\|$\)/\1,goto:c1\2/g'`
   else
      strt_cntl=$strt_cntl,repeat
   fi
fi
unset strt_cond stop_cond stop_cntl
if [ -n "$mdl_addr" ] && [ ${mdl_addr:0:1} == - ];then
   strt_cntl=`echo $strt_cntl|awk -F ':address:' '{ORS="";print $1;for(i=2;i<=NF;i++){print":address:";split($i,A,",");split(A[1],B,":");printf("'"$mdl_name+"'0x%x",strtonum(B[1])'"$mdl_addr"');for(k in B){if(k!=1){print":"B[k]}}for(j in A){if(j!=1){print","A[j]}}}ORS="\n";print""}'`
fi
if [ $mp_mode == true ];then
   if [ "$action" == pinlit2 ] || [ "$action" == record ];then
      args="${args:+$args }"'-log:pid'
   elif [ "$action" == stats ] || [ "$action" == mix ];then
      if [ -n "$cur_rank" ];then
         printf -v mrk '%05d' $cur_rank
         for log in $logs;do
            key=${log%%:*};def=${log#*:}
            pair=$key\\\|-`echo ${key:1}|tr - _`
            save_args=$args;args=`echo $save_args|sed 's/\(^\|\s\)\('$pair'\)\(\s\+\S\+\.\)\([^. ]\+\)/\1\2\3'$mrk'.\4/g'`
            if [ "$args" == "$save_args" ];then
               args="${args:+$args }""$key ${def%.*}.$mrk.${def##*.}"
            fi
         done
         unset save_args pair key log mrk
      else
         args="${args:+$args }"'-pid'
      fi
   fi
else
   if   ( [ -n "$replay" ] && [ $mt_mode != true ] ) || ( [ $log_shm == true ] && [ -z "$tid" ] );then
      args="${args:+$args }"'-log:mt 0'
   elif ( [ -n "$replay" ] && [ $mt_mode == true ] ) || [ "$action" == pinlit2 ] || [ "$action" == warmup ] || [ "$action" == record ];then
      args="${args:+$args }"'-log:mt'
   fi
fi
if [ "$action" == mix ];then
   re='-top[-_]blocks\s+[0-9]+';if [[ ! $args =~ $re ]];then args="${args:+$args }-top-blocks ${TRACING_SDK_RUN_SDE_BLKS:-521}"; fi
   if [ $repeat != true ] && [ -z "$replay" ];then args="${args:+$args }"'-mix-verbose'; fi
   if [ -n "$replay" ];then
      if [[ ! $args =~ -pinplay:msgfile ]];then
         msgs=`echo $replay|sed 's/\.[0-9]\+$//'`.$action
         args=`echo $args|sed 's| -replay:basename | -pinplay:msgfile '$msgs' -replay:basename |'`
         unset msgs
      fi
   else
      re='-odir\s';if [[ ! $args =~ $re ]];then
         args="${args:+$args }"'-odir rgn.pinball'
         dir_reqd=true
      fi
      args="${args:+$args }"'-follow-subprocess'
   fi
fi
re='(^|\s)-clc(\s|$)';if [[ $args =~ $re ]] && [ `$atool -mix -thelp|grep -m 1 -c '\-clc'` -eq 0 ];then #!!! delete, when SDE for LRB2 supports -clc
   args=`echo $args|sed 's/\(^\|\s\+\)\(-clc\|-clc-out\s\+\S\+\)//g'`
fi
send()
{
   if [ -n "$comm" ];then
      case "$comm" in
      /dev/stdout)
         echo "$@" ;;
      /dev/stderr)
         echo "$@" >&2 ;;
      *)
         echo "$@" >>"$comm" ;;
      esac
   fi
}
store_env()
{
   if [ -n "$env" ];then
      file=''
      tools='icc icpc ifort'
      for t in $tools;do
         if [ -n "`which $t 2>/dev/null`" ];then
            if [ -z "$file" ];then
               file=$env
               uname -a >$file
            fi
            echo ";--- $t" >>$file
            $t -V 2>&1|sed '/^$/d' >>$file
         fi
      done
      if [ -n "$file" ];then
         if [ $action == profile ];then
            echo ';--- get_prf' >>$file
            $base/get_prf --version|sed '/^$/d' >>$file
         else
            echo ';--- sde' >>$file
            $atool --version|sed '/^$/d' >>$file
         fi
         echo ';--- ulimit' >>$file
         ulimit -a >>$file
         echo ';--- env' >>$file
         env >>$file
      fi
      unset t tools file
   fi
}
run()
{
   unset re run store_env send base atool etool cur_rank rank native action mp_mode mt_mode log_shm dir_reqd delay comm args tgt mdl_name mdl_addr env tid logs replay pause strt_cond strt_cntl stop_cond stop_cntl largs repeat cur_cond rt_cntl
   if [ $fixed != true ];then
      unset fixed
      if [ -n "$stdin" ];then
         __std_in__="$stdin";unset stdin
         exec "$@" < "$__std_in__"
      else
         exec "$@"
      fi
   else
      unset fixed
      if [ -n "$stdin" ];then
         __std_in__="$stdin";unset stdin
         LD_BIND_NOW=true exec "$@" < "$__std_in__"
      else
         LD_BIND_NOW=true exec "$@"
      fi
   fi
}
if [ $mt_mode != true ] && ( [ $action == pinlit2 ] || [ $action == record ] );then
   if [ -n "$cur_rank" ] && [ $cur_rank -eq $rank ];then
      if [ -z "$OMP_NUM_THREADS" ] || [ "$OMP_NUM_THREADS" -eq 1 ];then unset pause; fi
   fi
fi
if [ $fixed == true ];then
   if [ $action == profile ];then
      args="${args:+$args }"'setarch x86_64 -R'
   else
      atool="setarch x86_64 -RL $atool"
   fi
fi
if [ $action != profile-mix ] && [ "$tgt" == -knc ];then unset tgt; fi
if [ $action != run ] && [ $action != profile ];then native=false; fi
if [ $native == true ];then
   shift
   if [ -z "$cur_rank" ] || [ $cur_rank -eq $rank ];then
      if [ -n "$rt_cntl" ];then export MPI_PCONTROL_TSC="$rt_cntl"; fi
   fi
   run "$@"
else
   if [ $action == profile ];then 
      shift
      largs=`echo $largs|sed 's/\(-mpi-rank\s\+[0-9]\+\|-store-env\)\s*//g'`
   fi
   if [ $action == profile ] || [ $action == profile-mix ];then
      args="$largs"
      unset strt_cntl
   fi
   if [ -z "$cur_rank" ] || [ $cur_rank -eq $rank ];then
      send $PWD
      store_env
      if [ $dir_reqd == true ];then
         rm -rf rgn.pinball
         mkdir rgn.pinball
      fi
      if [ $log_shm == true ];then
         $atool -pinlit2 -log:mp-delete-pool -log:basename rgn.pinball/pool "$@"
         $atool -pinlit2 -log:mp-create-pool -log:basename rgn.pinball/pool "$@"
         sleep $(($delay*9/10))s
      fi
      if [ -n "$__SDE_TBD_NAME" ];then export __SDE_TBD_NAME; fi
      run $atool ${tgt:+$tgt }${pause:+$pause }${strt_cntl:+$strt_cntl }$args "$@"
   elif [ $mp_mode == true ];then
      run $etool ${tgt:+$tgt }`echo $args|sed 's/\(\s*\)-stop-address-relative\s\+\S\+\s*/\1/g'|sed 's/\(\s*\)-length\s\+\S\+\s*/\1/g'|sed 's/\(\s*\)-\(log:\|\w\+-\)\?early-out\(\s\+[0-9]\+\)\?/\1-\2early-out 0/g'` "$@"
   else
      if [ $log_shm == true ];then
         #mm_name=`mktemp -ut pinplay_mmap_file_${USER}_XXXX|sed 's/_.\{4\}$//'`
         mm_name=/tmp/pinplay_mmap_file_${USER} #delete when SDE fixed
         mm_time=`stat -c %Y $mm_name 2>/dev/null`
         sleep ${delay}s
         if [ -e $mm_name ] && [ "`stat -c %Y $mm_name 2>/dev/null`" != "$mm_time" ];then
            unset mm_name mm_time
            run $atool ${tgt:+$tgt }-pinlit2 ${pause:+$pause }-start-int3 10000000 -log:mp-mode-lock-only -log:mp-attach -log:mt -log:focus-thread 0 -log:basename rgn.pinball/procs/lock -log:pid -logfile rgn.pinball/procs/pin-tool-log_$$.txt "$@"
            exit #will never be executed
         fi
         unset mm_name mm_time
      fi
      run $etool ${tgt:+$tgt }${pause:+$pause }"$@"
   fi
fi
